Imports Aricie.Collections
Imports System.ComponentModel
Imports Aricie.DNN.UI.Attributes
Imports Aricie.ComponentModel
Imports DotNetNuke.UI.WebControls
Imports System.Xml.Serialization
Imports Aricie.Services
Imports Aricie.DNN.Services.Workers
Imports Aricie.DNN.UI.WebControls
Imports Aricie.DNN.UI.WebControls.EditControls
Imports DotNetNuke.UI.Skins.Controls

Namespace Aricie.DNN.Caching
    <Serializable()> _
    Public Class CloudSettings

#Region "Entity members"



        Private _EnableSynchronization As Boolean = True

        Private _EnableDistributedCache As Boolean
        Private _DisableGroupStrategies As Boolean
        Private _DisableIndividualStrategies As Boolean
        Private _NoCatalog As Boolean = True
        Private _DefaultSerializationMode As CacheDistributionType = CacheDistributionType.Serialized
        Private _EnableVariableKeys As Boolean = True
        Private _EnableCompression As Boolean = True
        Private _SkipIConvertible As Boolean
        Private _EnforceXmlSerialization As Boolean
        Private _StartupKeys As New List(Of String)

        'Private _NbStrategiesInUse As Integer
        Private _DefaultKeyConfig As New CacheStrategy(True, "{0}", CacheDistributionType.NotDistributed, False, ConcurrencyMode.NoConcurrency, 50, "", _
                                                      "", True, BatchMode.NoBatch, "", "", TimeOutMode.Unchanged, 1, False, False, True, False, False, False, True)

        Private _ResetTimeOutTaskQueueInfo As New TaskQueueInfo(1, True, TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(100))
        Private _DistributedCachingInsertTaskQueueInfo As New TaskQueueInfo(1, True, TimeSpan.FromSeconds(3), TimeSpan.FromMilliseconds(400), TimeSpan.FromMilliseconds(50))
        Private _SynchronizerPushTaskQueueInfo As New TaskQueueInfo(1, True, TimeSpan.FromSeconds(3), TimeSpan.FromMilliseconds(400), TimeSpan.FromMilliseconds(30))

        'Private _SynchronizerPullTaskQueueInfo As New TaskQueueInfo(1, True, TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(200), TimeSpan.FromMilliseconds(30))
        Private _LocalInsertTaskQueueInfo As New TaskQueueInfo(1, True, TimeSpan.FromMilliseconds(200), TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(10))
        Private _GlobalInsertTaskQueueInfo As New TaskQueueInfo(1, True, TimeSpan.FromMilliseconds(200), TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(10))
        Private _BatchGetTaskQueueInfo As New TaskQueueInfo(1, True, TimeSpan.Zero, TimeSpan.Zero, TimeSpan.Zero)
        Private _GetTaskQueueInfo As New TaskQueueInfo(1, True, TimeSpan.Zero, TimeSpan.Zero, TimeSpan.Zero)

        Private _BroadCastKey As String = "DNNBroadCast"
        Private _BroadCastStrategy As New CacheStrategy(True, "{0}", CacheDistributionType.Serialized, False, ConcurrencyMode.NoConcurrency, 50, "", _
                                "DNNSynchro", False, BatchMode.NoBatch, "DNNSynchro", "", TimeOutMode.NoTimeOut, 1, True, True, False, False, False, True, False)

        Private _DependencyKeysPrefix As String = "Dep-"
        Private _DependencyKeysConfig As New CacheStrategy(True, "{0}", CacheDistributionType.Serialized, False, ConcurrencyMode.NoConcurrency, 50, "", _
                                "DNNSynchro", False, BatchMode.NoBatch, "DNNSynchro", "", TimeOutMode.NoTimeOut, 1, True, True, False, True, True, True, False)
        Private _GlobalDependencyKey As String = "GlobalDistributedCaching"
        Private _GlobalDependencyConfig As New CacheStrategy(True, "{0}", CacheDistributionType.NotDistributed, False, ConcurrencyMode.NoConcurrency, 50, "", _
                                "DNNSynchro", False, BatchMode.NoBatch, "DNNSynchro", "", TimeOutMode.NoTimeOut, 1, True, False, False, False, True, False, True)
        Private _CatalogKey As String = "ClusterCatalog"
        Private _CatalogKeyConfig As New CacheStrategy(True, "{0}", CacheDistributionType.Serialized, True, ConcurrencyMode.NoConcurrency, 50, "", _
                                "DNNSynchro", False, BatchMode.Bundle, "DNNSynchro", "Startup", TimeOutMode.NoTimeOut, 1, True, False, True, False, False, False, True)


        Private _BundlePrefix As String = "VBundle-"
        Private _BundleStrategy As New CacheStrategy(True, "{0}", CacheDistributionType.Serialized, False, ConcurrencyMode.NoConcurrency, 50, "", _
                                "", False, BatchMode.NoBatch, "", "", TimeOutMode.NoTimeOut, 1, True, False, True, False, False, False, True)
        Private _TagsCatalogKey As String = "TagsCatalog"
        Private _TagsCatalogKeyConfig As New CacheStrategy(True, "{0}", CacheDistributionType.Serialized, False, ConcurrencyMode.NoConcurrency, 50, "", _
                                "DNNSynchro", False, BatchMode.NoBatch, "DNNSynchro", "", TimeOutMode.NoTimeOut, 1, True, False, True, False, False, False, False)
        Private _GroupStrategies As New List(Of CacheGroupInfo)
        Private _IndividualStrategies As New SerializableDictionary(Of String, CacheStrategy)


        Public Property EnableSynchronization() As Boolean
            Get
                Return _EnableSynchronization
            End Get
            Set(ByVal value As Boolean)
                _EnableSynchronization = value
            End Set
        End Property

        Public Property EnableDistributedCache() As Boolean
            Get
                Return _EnableDistributedCache
            End Get
            Set(ByVal value As Boolean)
                _EnableDistributedCache = value
            End Set
        End Property

        Public ReadOnly Property NbKeysConfigured() As Integer
            Get
                Dim toReturn As Integer = Me._IndividualStrategies.Count
                For Each objGroup As CacheGroupInfo In Me._GroupStrategies.Where(Function(objStrat) objStrat.Enabled)
                    toReturn += objGroup.Keys.Count
                Next
                Return toReturn
            End Get
        End Property

        Public ReadOnly Property NbStrategiesInUse() As Integer
            Get
                Return Me.GetDefaultJobs.Count
            End Get
        End Property

        Public Property DisableGroupStrategies() As Boolean
            Get
                Return _DisableGroupStrategies
            End Get
            Set(ByVal value As Boolean)
                _DisableGroupStrategies = value
            End Set
        End Property

        Public Property DisableIndividualStrategies() As Boolean
            Get
                Return _DisableIndividualStrategies
            End Get
            Set(ByVal value As Boolean)
                _DisableIndividualStrategies = value
            End Set
        End Property

       
        

        <ExtendedCategory("DefaultStrategy")> _
            <Editor(GetType(PropertyEditorEditControl), GetType(EditControl))> _
            <LabelMode(LabelMode.Top)> _
        Public Property DefaultKeyConfig() As CacheStrategy
            Get
                Return _DefaultKeyConfig
            End Get
            Set(ByVal value As CacheStrategy)
                _DefaultKeyConfig = value
            End Set
        End Property

        <ExtendedCategory("UserStrategies", "GroupStrategies")> _
        Public Property GroupStrategies() As List(Of CacheGroupInfo)
            Get
                Return _GroupStrategies
            End Get
            Set(ByVal value As List(Of CacheGroupInfo))
                _GroupStrategies = value
            End Set
        End Property

        <ExtendedCategory("UserStrategies", "IndividualStrategies")> _
         <KeyEditor(GetType(CustomTextEditControl), GetType(IndividualStrategiesKeyAttributes))> _
         <ValueEditor(GetType(PropertyEditorEditControl))> _
        Public Property IndividualStrategies() As SerializableDictionary(Of String, CacheStrategy)
            Get
                Return _IndividualStrategies
            End Get
            Set(ByVal value As SerializableDictionary(Of String, CacheStrategy))
                _IndividualStrategies = value
            End Set
        End Property

        <ExtendedCategory("AdvancedSettings")> _
        Public Property NoCatalog() As Boolean
            Get
                Return _NoCatalog
            End Get
            Set(ByVal value As Boolean)
                _NoCatalog = value
            End Set
        End Property

        <ExtendedCategory("AdvancedSettings")> _
        Public Property DefaultSerializationMode() As CacheDistributionType
            Get
                Return _DefaultSerializationMode
            End Get
            Set(ByVal value As CacheDistributionType)
                _DefaultSerializationMode = value
            End Set
        End Property

        <ExtendedCategory("AdvancedSettings")> _
        Public Property EnableVariableKeys() As Boolean
            Get
                Return _EnableVariableKeys
            End Get
            Set(ByVal value As Boolean)
                _EnableVariableKeys = value
            End Set
        End Property

        <ExtendedCategory("AdvancedSettings")> _
        Public Property EnableCompression() As Boolean
            Get
                Return _EnableCompression
            End Get
            Set(ByVal value As Boolean)
                _EnableCompression = value
            End Set
        End Property

        <ExtendedCategory("AdvancedSettings")> _
        Public Property SkipIConvertible() As Boolean
            Get
                Return _SkipIConvertible
            End Get
            Set(ByVal value As Boolean)
                _SkipIConvertible = value
            End Set
        End Property


        <ExtendedCategory("AdvancedSettings")> _
        Public Property EnforceXmlSerialization() As Boolean
            Get
                Return _EnforceXmlSerialization
            End Get
            Set(ByVal value As Boolean)
                _EnforceXmlSerialization = value
            End Set
        End Property


        <ExtendedCategory("AdvancedSettings")> _
       <XmlIgnore()> _
       <Editor(GetType(CustomTextEditControl), GetType(EditControl))> _
       <LineCount(10)> _
       <Width(500)> _
       <LabelMode(LabelMode.Top)> _
        Public Property StringStartupKeys() As String
            Get
                Return CacheStrategy.FormatKeys(Me._StartupKeys.ToArray)
            End Get
            Set(ByVal value As String)
                Me._StartupKeys = New List(Of String)(value.Trim.Trim(","c).Split(CacheGroupInfo.glbKeysSeparator, StringSplitOptions.RemoveEmptyEntries))
            End Set
        End Property


        <Browsable(False)> _
        Public Property StartupKeys() As List(Of String)
            Get
                Return _StartupKeys
            End Get
            Set(ByVal value As List(Of String))
                _StartupKeys = value
            End Set
        End Property

        <ExtendedCategory("AdvancedSettings")> _
        Public Property ClearRegionName As String = ""

        <ExtendedCategory("AdvancedSettings")> _
        <ActionButton(IconName.TrashO, IconOptions.Normal, "Reset.Warning")> _
        Public Overridable Overloads Sub ClearRegion(pe As AriciePropertyEditorControl)
            If ClearRegionName = "" Then
                DistributedCachingProviderBase.ClearRegions()
            Else
                DistributedCachingProviderBase.ClearRegion(ClearRegionName)
            End If
            pe.ItemChanged = True
            pe.DisplayLocalizedMessage("RegionsReset.Message", ModuleMessage.ModuleMessageType.GreenSuccess)
        End Sub

        <ExtendedCategory("AdvancedSettings")> _
        <ActionButton(IconName.TrashO, IconOptions.Normal, "Reset.Warning")> _
        Public Overridable Overloads Sub DeleteRegion(pe As AriciePropertyEditorControl)
            If ClearRegionName = "" Then
                DistributedCachingProviderBase.DeleteRegions()
            Else
                DistributedCachingProviderBase.DeleteRegion(ClearRegionName)
            End If
            pe.ItemChanged = True
            pe.DisplayLocalizedMessage("RegionsDeleted.Message", ModuleMessage.ModuleMessageType.GreenSuccess)
        End Sub


        <ExtendedCategory("EngineSettings")> _
    <Editor(GetType(PropertyEditorEditControl), GetType(EditControl))> _
    <LabelMode(LabelMode.Top)> _
        Public Property GetTaskQueueInfo() As TaskQueueInfo
            Get
                Return _GetTaskQueueInfo
            End Get
            Set(ByVal value As TaskQueueInfo)
                _GetTaskQueueInfo = value
            End Set
        End Property

        <ExtendedCategory("EngineSettings")> _
           <Editor(GetType(PropertyEditorEditControl), GetType(EditControl))> _
           <LabelMode(LabelMode.Top)> _
        Public Property BatchGetTaskQueueInfo() As TaskQueueInfo
            Get
                Return _BatchGetTaskQueueInfo
            End Get
            Set(ByVal value As TaskQueueInfo)
                _BatchGetTaskQueueInfo = value
            End Set
        End Property

        <ExtendedCategory("EngineSettings")> _
           <Editor(GetType(PropertyEditorEditControl), GetType(EditControl))> _
           <LabelMode(LabelMode.Top)> _
        Public Property GlobalInsertTaskQueueInfo() As TaskQueueInfo
            Get
                Return _GlobalInsertTaskQueueInfo
            End Get
            Set(ByVal value As TaskQueueInfo)
                _GlobalInsertTaskQueueInfo = value
            End Set
        End Property


        <ExtendedCategory("EngineSettings")> _
           <Editor(GetType(PropertyEditorEditControl), GetType(EditControl))> _
           <LabelMode(LabelMode.Top)> _
        Public Property LocalInsertTaskQueueInfo() As TaskQueueInfo
            Get
                Return _LocalInsertTaskQueueInfo
            End Get
            Set(ByVal value As TaskQueueInfo)
                _LocalInsertTaskQueueInfo = value
            End Set
        End Property

        '<ExtendedCategory("EngineSettings")> _
        '   <Editor(GetType(PropertyEditorEditControl), GetType(EditControl))> _
        '   <LabelMode(LabelMode.Top)> _
        'Public Property SynchronizerPullTaskQueueInfo() As TaskQueueInfo
        '    Get
        '        Return _SynchronizerPullTaskQueueInfo
        '    End Get
        '    Set(ByVal value As TaskQueueInfo)
        '        _SynchronizerPullTaskQueueInfo = value
        '    End Set
        'End Property




        <ExtendedCategory("EngineSettings")> _
           <Editor(GetType(PropertyEditorEditControl), GetType(EditControl))> _
           <LabelMode(LabelMode.Top)> _
        Public Property SynchronizerPushTaskQueueInfo() As TaskQueueInfo
            Get
                Return _SynchronizerPushTaskQueueInfo
            End Get
            Set(ByVal value As TaskQueueInfo)
                _SynchronizerPushTaskQueueInfo = value
            End Set
        End Property

        <ExtendedCategory("EngineSettings")> _
        Public Property SynchronizerFailoverTaskQueueInfo() As New TaskQueueInfo(1, True, TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(3), TimeSpan.FromMilliseconds(300))
            


        <ExtendedCategory("EngineSettings")> _
           <Editor(GetType(PropertyEditorEditControl), GetType(EditControl))> _
           <LabelMode(LabelMode.Top)> _
        Public Property DistributedCachingInsertTaskQueueInfo() As TaskQueueInfo
            Get
                Return _DistributedCachingInsertTaskQueueInfo
            End Get
            Set(ByVal value As TaskQueueInfo)
                _DistributedCachingInsertTaskQueueInfo = value
            End Set
        End Property


        <ExtendedCategory("EngineSettings")> _
           <Editor(GetType(PropertyEditorEditControl), GetType(EditControl))> _
           <LabelMode(LabelMode.Top)> _
        Public Property ResetTimeOutTaskQueueInfo() As TaskQueueInfo
            Get
                Return _ResetTimeOutTaskQueueInfo
            End Get
            Set(ByVal value As TaskQueueInfo)
                _ResetTimeOutTaskQueueInfo = value
            End Set
        End Property

        


        <ExtendedCategory("SystemStrategies", "Synchronization")> _
        <Required(True)> _
        Public Property DependencyKeysPrefix() As String
            Get
                Return _DependencyKeysPrefix
            End Get
            Set(ByVal value As String)
                _DependencyKeysPrefix = value
            End Set
        End Property

        <ExtendedCategory("SystemStrategies", "Synchronization")> _
           <Editor(GetType(PropertyEditorEditControl), GetType(EditControl))> _
           <LabelMode(LabelMode.Top)> _
        Public Property DependencyKeysConfig() As CacheStrategy
            Get
                Return _DependencyKeysConfig
            End Get
            Set(ByVal value As CacheStrategy)
                _DependencyKeysConfig = value
            End Set
        End Property

        <ExtendedCategory("SystemStrategies", "GlobalDependency")> _
        <Required(True)> _
        Public Property GlobalDependencyKey() As String
            Get
                Return _GlobalDependencyKey
            End Get
            Set(ByVal value As String)
                _GlobalDependencyKey = value
            End Set
        End Property


        <ExtendedCategory("SystemStrategies", "GlobalDependency")> _
           <Editor(GetType(PropertyEditorEditControl), GetType(EditControl))> _
           <LabelMode(LabelMode.None)> _
        Public Property GlobalDependencyConfig() As CacheStrategy
            Get
                Return _GlobalDependencyConfig
            End Get
            Set(ByVal value As CacheStrategy)
                _GlobalDependencyConfig = value
            End Set
        End Property

        <ExtendedCategory("SystemStrategies", "Catalog")> _
       <Required(True)> _
        Public Property CatalogKey() As String
            Get
                Return _CatalogKey
            End Get
            Set(ByVal value As String)
                _CatalogKey = value
            End Set
        End Property


        <ExtendedCategory("SystemStrategies", "Catalog")> _
           <Editor(GetType(PropertyEditorEditControl), GetType(EditControl))> _
           <LabelMode(LabelMode.Top)> _
         Public Property CatalogKeyConfig() As CacheStrategy
            Get
                Return _CatalogKeyConfig
            End Get
            Set(ByVal value As CacheStrategy)
                _CatalogKeyConfig = value
            End Set
        End Property

        <XmlIgnore()> _
            <ExtendedCategory("SystemStrategies", "Catalog")> _
            <LabelMode(LabelMode.Top)> _
        Public ReadOnly Property CatalogKeys() As String
            Get
                Dim sortedKeys As New List(Of String)(DistributedCachingProviderBase.ClusterCatalog.Keys)
                sortedKeys.Sort()
                Return CacheStrategy.FormatKeys(sortedKeys.ToArray)
            End Get
        End Property

        <ExtendedCategory("SystemStrategies", "Broadcast")> _
       <Required(True)> _
        Public Property BroadcastKey() As String
            Get
                Return _BroadCastKey
            End Get
            Set(ByVal value As String)
                _BroadCastKey = value
            End Set
        End Property


        <ExtendedCategory("SystemStrategies", "Broadcast")> _
           <Editor(GetType(PropertyEditorEditControl), GetType(EditControl))> _
           <LabelMode(LabelMode.Top)> _
        Public Property BroadCastStrategy() As CacheStrategy
            Get
                Return _BroadCastStrategy
            End Get
            Set(ByVal value As CacheStrategy)
                _BroadCastStrategy = value
            End Set
        End Property

        <ExtendedCategory("SystemStrategies", "BundleSettings")> _
        Public Property BundlePrefix() As String
            Get
                Return _BundlePrefix
            End Get
            Set(ByVal value As String)
                _BundlePrefix = value
            End Set
        End Property

        <ExtendedCategory("SystemStrategies", "BundleSettings")> _
           <Editor(GetType(PropertyEditorEditControl), GetType(EditControl))> _
           <LabelMode(LabelMode.Top)> _
        Public Property BundleStrategy() As CacheStrategy
            Get
                Return _BundleStrategy
            End Get
            Set(ByVal value As CacheStrategy)
                _BundleStrategy = value
            End Set
        End Property

        <ExtendedCategory("SystemStrategies", "BundleSettings")> _
       <Required(True)> _
        Public Property TagsCatalogKey() As String
            Get
                Return _TagsCatalogKey
            End Get
            Set(ByVal value As String)
                _TagsCatalogKey = value
            End Set
        End Property


        <ExtendedCategory("SystemStrategies", "BundleSettings")> _
        <LabelMode(LabelMode.Top)> _
        Public Property TagsCatalogKeyConfig() As CacheStrategy
            Get
                Return _TagsCatalogKeyConfig
            End Get
            Set(ByVal value As CacheStrategy)
                _TagsCatalogKeyConfig = value
            End Set
        End Property

       


        Public Class IndividualStrategiesKeyAttributes
            Implements IAttributesProvider


            Public Function GetAttributes() As System.Collections.Generic.IEnumerable(Of System.Attribute) Implements IAttributesProvider.GetAttributes
                Dim toReturn As New List(Of Attribute)
                toReturn.Add(New WidthAttribute(400))
                Return toReturn
            End Function
        End Class

       

#End Region


#Region "Job Members"

        Private _XmlSerializableTests As SerializableDictionary(Of String, Boolean)
        Private _DefaultJob As CacheJob
        Private _DependencyKeysJob As CacheJob
        Private _GlobalDependencyJob As CacheJob

        Private _CatalogJob As CacheJob
        Private _TagsCatalogJob As CacheJob
        Private _BundleJob As CacheJob
        Private _BroadcastJob As CacheJob


        '     <NonSerialized()> _
        'Private _GlobalDependencyPutJob As CachePutJob
        Private _DependencyKeysPutJob As CachePutJob

        Private _TempKeyJobs As Dictionary(Of String, CacheJob)


        <XmlIgnore()> _
      <Browsable(False)> _
        Public ReadOnly Property XmlSerializableTests() As SerializableDictionary(Of String, Boolean)
            Get
                If _XmlSerializableTests Is Nothing Then
                    _XmlSerializableTests = New SerializableDictionary(Of String, Boolean)
                End If
                Return _XmlSerializableTests
            End Get
        End Property



        <XmlIgnore()> _
      <Browsable(False)> _
      Public ReadOnly Property DefaultJob() As CacheJob
            Get
                If _DefaultJob.Strategy Is Nothing Then
                    _DefaultJob = New CacheJob(StrategyType.Default, Me._DefaultKeyConfig, "")
                End If
                Return _DefaultJob
            End Get
        End Property

        <XmlIgnore()> _
    <Browsable(False)> _
    Public ReadOnly Property DependencyKeysPutJob() As CachePutJob
            Get
                If _DependencyKeysPutJob.Value Is Nothing Then

                    _DependencyKeysPutJob = New CachePutJob(Me.DependencyKeysJob, New WrappedDistributedCacheObject(True, Me.DependencyKeysJob))
                End If
                Return _DependencyKeysPutJob
            End Get
        End Property


        <XmlIgnore()> _
     <Browsable(False)> _
     Public ReadOnly Property DependencyKeysJob() As CacheJob
            Get
                If _DependencyKeysJob.Strategy Is Nothing Then
                    _DependencyKeysJob = New CacheJob(StrategyType.Key, Me._DependencyKeysConfig, Me._DependencyKeysPrefix)
                End If
                Return _DependencyKeysJob
            End Get
        End Property

        <XmlIgnore()> _
      <Browsable(False)> _
      Public ReadOnly Property GlobalDependencyJob() As CacheJob
            Get
                If _GlobalDependencyJob.Strategy Is Nothing Then
                    _GlobalDependencyJob = New CacheJob(StrategyType.Default, Me._GlobalDependencyConfig, Me._GlobalDependencyKey)
                End If
                Return _GlobalDependencyJob
            End Get
        End Property


        <XmlIgnore()> _
        <Browsable(False)> _
        Public ReadOnly Property CatalogJob() As CacheJob
            Get
                If _CatalogJob.Strategy Is Nothing Then
                    _CatalogJob = New CacheJob(StrategyType.Default, Me._CatalogKeyConfig, Me._CatalogKey)
                End If
                Return _CatalogJob
            End Get
        End Property

        <XmlIgnore()> _
        <Browsable(False)> _
        Public ReadOnly Property TagsCatalogJob() As CacheJob
            Get
                If _TagsCatalogJob.Strategy Is Nothing Then
                    _TagsCatalogJob = New CacheJob(StrategyType.Default, Me._TagsCatalogKeyConfig, Me._TagsCatalogKey)
                End If
                Return _TagsCatalogJob
            End Get
        End Property

        <XmlIgnore()> _
       <Browsable(False)> _
       Public ReadOnly Property BundleJob() As CacheJob
            Get
                If _BundleJob.Strategy Is Nothing Then
                    _BundleJob = New CacheJob(StrategyType.Default, Me._BundleStrategy, Me._BundlePrefix)
                End If
                Return _BundleJob
            End Get
        End Property

        <XmlIgnore()> _
      <Browsable(False)> _
      Public ReadOnly Property BroadcastJob() As CacheJob
            Get
                If _BroadcastJob.Strategy Is Nothing Then
                    _BroadcastJob = New CacheJob(StrategyType.Default, Me._BroadCastStrategy, Me._BroadCastKey)
                End If
                Return _BroadcastJob
            End Get
        End Property

        '<XmlIgnore()> _
        '<Browsable(False)> _
        'Public ReadOnly Property GlobalDependencyPutJob() As CachePutJob
        '    Get
        '        If _GlobalDependencyPutJob.Value Is Nothing Then
        '            _GlobalDependencyPutJob = New CachePutJob(GlobalDependencyJob, New WrappedDistributedCacheObject(True, Me.GlobalDependencyJob))
        '        End If
        '        Return _GlobalDependencyPutJob
        '    End Get
        'End Property

        Public Function GetGroupJob(ByVal objGroup As CacheGroupInfo) As CacheJob
            Return New CacheJob(StrategyType.Group, objGroup, "")
        End Function




        Public Function GetGroupJobs() As List(Of CacheJob)
            Dim toReturn As New List(Of CacheJob)
            For Each objGroup As CacheGroupInfo In Me.GroupStrategies
                If objGroup.Enabled Then
                    toReturn.Add(Me.GetGroupJob(objGroup))
                End If
            Next
            Return toReturn
        End Function

        Public Function GetDefaultJobs() As List(Of CacheJob)
            Dim toReturn As New List(Of CacheJob)(4 + Me._IndividualStrategies.Count + Me._GroupStrategies.Where(Function(objStrat) objStrat.Enabled).Count)
            toReturn.Add(Me.DefaultJob)
            toReturn.Add(Me.GlobalDependencyJob)
            toReturn.Add(Me.CatalogJob)
            toReturn.Add(Me.TagsCatalogJob)
            toReturn.Add(Me.DependencyKeysJob)
            toReturn.Add(Me.BundleJob)
            toReturn.Add(Me.BroadcastJob)
            If Not Me._DisableGroupStrategies Then
                toReturn.AddRange(Me.GetGroupJobs)
            End If
            If Not Me._DisableIndividualStrategies Then
                For Each objPair As KeyValuePair(Of String, CacheStrategy) In Me._IndividualStrategies
                    If objPair.Value.Enabled Then
                        toReturn.Add(New CacheJob(StrategyType.Key, objPair.Value, objPair.Key))
                    End If
                Next
            End If
            Return toReturn
        End Function


        Public Function GetAllJobs(ByVal keyLessGroupJobs As Boolean) As Dictionary(Of String, CacheJob)
            Dim allJobs As List(Of CacheJob) = Me.GetDefaultJobs
            Dim toReturn As New Dictionary(Of String, CacheJob)(allJobs.Count + Me.NbKeysConfigured)
            For Each job As CacheJob In allJobs
                Select Case job.StrategyType
                    Case StrategyType.Default, StrategyType.Key
                        toReturn.Add(job.Key, job)
                    Case StrategyType.Group
                        For Each objKey As String In DirectCast(job.Strategy, CacheGroupInfo).Keys
                            If keyLessGroupJobs Then
                                toReturn(objKey) = job
                            Else
                                toReturn(objKey) = New CacheJob(objKey, job)
                            End If
                        Next
                End Select
            Next
            Return toReturn
        End Function

        <XmlIgnore()> _
        <Browsable(False)> _
        Private ReadOnly Property TempKeyJobs() As Dictionary(Of String, CacheJob)
            Get
                If _TempKeyJobs Is Nothing Then
                    _TempKeyJobs = New Dictionary(Of String, CacheJob)(Me.GetAllJobs(False))
                End If
                Return _TempKeyJobs
            End Get
        End Property


        Public Function GetKeyJob(ByVal key As String) As CacheJob
            Dim toReturn As CacheJob = Nothing
            If Not TempKeyJobs.TryGetValue(key, toReturn) Then
                Dim found As Boolean
                If Me._EnableVariableKeys AndAlso key <> glbVarRegexCacheKey Then
                    Dim partialMatch As Boolean
                    Dim candidates As IList(Of VariableStrategy(Of CacheJob)) = VariablePatterns.Search(key, partialMatch)
                    If candidates IsNot Nothing Then
                        For Each candidate As VariableStrategy(Of CacheJob) In candidates
                            If candidate.Regex.IsMatch(key) Then
                                toReturn = New CacheJob(key, candidate.Strategy)
                                found = True
                                Exit For
                            End If
                        Next
                    End If
                End If
                If Not found Then
                    If DefaultJob.Strategy.DistributionType = CacheDistributionType.Undefined Then
                        'we will choose the distribution type later on so we need a clone
                        toReturn = New CacheJob(StrategyType.Key, ReflectionHelper.CloneObject(DefaultJob.Strategy), key)
                    Else
                        toReturn = New CacheJob(StrategyType.Key, DefaultJob.Strategy, key)
                    End If
                End If
                SyncLock _TempKeyJobs
                    _TempKeyJobs.Item(key) = toReturn
                End SyncLock
            End If
            Return toReturn
        End Function

        Public Function GetDependencyJob(ByVal objJob As CacheJob) As CacheJob
            Dim depKey As String = GetDepKeyFromKey(objJob.Key)
            Return New CacheJob(depKey, Me.DependencyKeysJob)
        End Function

        Public Function GetDependencyPutJob(ByVal objJob As CacheJob) As CachePutJob
            Dim depKey As String = GetDepKeyFromKey(objJob.Key)
            Return New CachePutJob(depKey, Me.DependencyKeysPutJob)
        End Function


        Public Function GetJobFromDependency(ByVal depKey As String) As CacheJob
            Dim key As String = GetKeyFromDepKey(depKey)
            Return GetKeyJob(key)
        End Function

        'Public Function GetPutJob(ByVal value As Object, ByVal keyJob As CacheJob) As CachePutJob
        '    'Dim clone As CacheJob = GetKeyJob(key)
        '    Dim wrapped As New WrappedDistributedCacheObject(value, keyJob)
        '    Return New CachePutJob(keyJob, wrapped)
        'End Function

        Public Function GetPutJob(ByVal keyJob As CacheJob, ByVal value As WrappedCacheObject) As CachePutJob
            'Dim clone As CacheJob = GetKeyJob(key)
            'Dim wrapped As New WrappedDistributedCacheObject(keyJob, value)
            Return New CachePutJob(keyJob, value)
        End Function

        Public Function GetBundleJob(ByVal objJob As CacheJob) As CacheJob
            Return New CacheJob(_BundlePrefix & objJob.Strategy.GetTags, Me.BundleJob)
        End Function

#Region "Private methods"

        Private Function GetDepKeyFromKey(ByVal key As String) As String
            Return Me._DependencyKeysPrefix & key

        End Function

        Private Function GetKeyFromDepKey(ByVal depKey As String) As String
            Return depKey.Substring(Me._DependencyKeysPrefix.Length)
        End Function



        Private Const glbVarRegexCacheKey As String = "VCPRegexs"

        Private _VariablePatterns As RadixTree(Of Char, String, IList(Of VariableStrategy(Of CacheJob)))


        Private ReadOnly Property VariablePatterns() As RadixTree(Of Char, String, IList(Of VariableStrategy(Of CacheJob)))
            Get

                If _VariablePatterns Is Nothing Then
                    '_VariablePatterns = DirectCast(DistributedCachingProvider.Instance.GetItem(glbVarRegexCacheKey), RadixTree(Of Char, String, IList(Of VariableStrategy(Of CacheJob))))

                    'If _VariablePatterns Is Nothing Then

                    _VariablePatterns = New RadixTree(Of Char, String, IList(Of VariableStrategy(Of CacheJob)))(7)

                    For Each objGroup As CacheJob In DistributedCachingProviderBase.Settings.CloudSettings.GetGroupJobs
                        For Each mapping As RegexMapping In DirectCast(objGroup.Strategy, CacheGroupInfo).VariableKeys
                            Try
                                Dim objTemplateCapture As New VariableStrategy(Of CacheJob)(mapping, objGroup)

                                Dim fixedPrefix As String = objTemplateCapture.Prefix
                                Dim prefixCollect As IList(Of VariableStrategy(Of CacheJob)) = _VariablePatterns.Find(fixedPrefix)
                                If prefixCollect Is Nothing Then
                                    prefixCollect = New List(Of VariableStrategy(Of CacheJob))
                                    _VariablePatterns.Set(fixedPrefix, prefixCollect)
                                End If
                                prefixCollect.Add(objTemplateCapture)
                            Catch ex As Exception
                                Aricie.DNN.Diagnostics.AsyncLogger.Instance.AddException(ex)
                            End Try
                        Next
                    Next

                    'DistributedCachingProvider.Instance.Insert(glbVarRegexCacheKey, _VariablePatterns, False)
                    'If Me._FriendlyContext.FriendlierUrlPortalSettings.LogCachedObjectCreation Then
                    '    Dim objPair As New KeyValuePair(Of String, String)("Nb Nodes:", toReturn.ItemCount.ToString)
                    '    Dim objDebug As New DebugInfo(Debug.DebugType, Debug.Labels.CreateVarRegexsRadixTree, objPair)
                    '    SimpleDebugLogger.Instance.AddDebugInfo(objDebug)
                    'End If
                End If
                'End If




                Return _VariablePatterns
            End Get
        End Property



        'Public Sub VariablePatternsRemovedHandler(ByVal key As String, ByVal value As Object, ByVal reason As CacheItemRemovedReason)
        '    _VariablePatterns = Nothing
        'End Sub

#End Region

#End Region

    End Class
End Namespace